#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "conndlg.h"
//#include "Icon/mysql.xpm"

IMPLEMENT_DYNAMIC_CLASS( ConnDlg, wxDialog )


BEGIN_EVENT_TABLE( ConnDlg, wxDialog )

    EVT_CHOICE( ID_LIST_SAVED, ConnDlg::OnListSavedSelected )

    EVT_BUTTON( ID_BT_CONN_SAVE, ConnDlg::OnBtConnSaveClick )

    EVT_BUTTON( ID_BT_CONN, ConnDlg::OnConnectBtnClick )

END_EVENT_TABLE()



ConnDlg::ConnDlg()
{
    Init();
}

ConnDlg::ConnDlg( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
    Init();
    Create(parent, id, caption, pos, size, style);
   	_ReadConfList(lsSaved,&array);
}

ConnDlg::ConnDlg( MYSQL* mysql,wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style ){
	this->mysql = mysql;
	Init();
    Create(parent, id, caption, pos, size, style);
   	_ReadConfList(lsSaved,&array);
}

ConnDlg::ConnDlg( ServerInfo* serverInfo, wxWindowID id , const wxString& caption, const wxPoint& pos, const wxSize& size, long style ){
	this->serverInfo = serverInfo;
	this->mysql = serverInfo->mysql;
	this->mysql = this->serverInfo->mysql;
	Init();
	Create(NULL,id,caption,pos,size,style);
   	_ReadConfList(lsSaved,&array);
}

bool ConnDlg::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
    SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
    wxDialog::Create( parent, id, caption, pos, wxSize(500,330), style );
    CreateControls();
    SetIcon(wxIcon(mysql_xpm));
	if (GetSizer())
    {
        GetSizer()->SetSizeHints(this);
    }
    Centre();
    return true;
}


ConnDlg::~ConnDlg()
{

}


void ConnDlg::Init()
{
    lsSaved = NULL;
    conn_host = NULL;
    conn_user = NULL;
    conn_pwd = NULL;
    conn_port = NULL;
    conn_db = NULL;
    conn_name = NULL;
}



void ConnDlg::CreateControls()
{    
	wxSplitterWindow* s = new wxSplitterWindow(this,wxID_ANY,wxDefaultPosition,GetSize(),wxBORDER_NONE );
	//s->SetSashGravity(1.0);

	wxImage image(topBar_xpm);
	image = image.Scale(GetSize().GetWidth()-GetWindowBorderSize().GetWidth()*2,60);
	wxBitmap img(image);
	wxStaticBitmap* topBar = new wxStaticBitmap(s,wxID_ANY,img);
	wxPanel* panel = new wxPanel(s,wxID_ANY);

	wxBoxSizer* topLevel = new wxBoxSizer(wxVERTICAL);
	wxStaticBox* staticBox = new wxStaticBox(panel,wxID_ANY, wxT("  Connect to MySQL Server Instance  "),wxDefaultPosition);
	wxStaticBoxSizer* staticSizer = new wxStaticBoxSizer(staticBox,wxVERTICAL);
	topLevel->Add(staticSizer, 0,wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
	panel->SetSizer(topLevel);

    wxPanel* itemDialog1 = panel;
	wxStaticBoxSizer* itemBoxSizer2 = staticSizer;
    /*wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);*/
    //itemDialog1->SetSizer(itemBoxSizer2);


    wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer2->Add(itemBoxSizer3, 0, wxGROW|wxALL, 5);

    wxBoxSizer* itemBoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer3->Add(itemBoxSizer4, 0, wxALIGN_CENTER_VERTICAL|wxLEFT, 5);

    wxStaticText* itemStaticText5 = new wxStaticText( itemDialog1, wxID_STATIC, _("Saved:   "), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer4->Add(itemStaticText5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 0);

    wxArrayString lsSavedStrings;
    lsSaved = new wxChoice( itemDialog1, ID_LIST_SAVED, wxDefaultPosition, wxDefaultSize, lsSavedStrings, wxBORDER_SIMPLE );
    itemBoxSizer3->Add(lsSaved, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxBoxSizer* itemBoxSizer7 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer2->Add(itemBoxSizer7, 0, wxGROW|wxALL, 5);

    wxStaticText* itemStaticText8 = new wxStaticText( itemDialog1, wxID_STATIC, _("Host:     "), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer7->Add(itemStaticText8, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    conn_host = new wxTextCtrl( itemDialog1, ID_CONN_HOST, _T(""), wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE );
    itemBoxSizer7->Add(conn_host, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText17 = new wxStaticText( itemDialog1, wxID_STATIC, _("Port:     "), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer7->Add(itemStaticText17, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    conn_port = new wxTextCtrl( itemDialog1, ID_CONN_PORT, _T(""), wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE );
    itemBoxSizer7->Add(conn_port, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxBoxSizer* itemBoxSizer10 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer2->Add(itemBoxSizer10, 0, wxGROW|wxALL, 5);

    wxStaticText* itemStaticText11 = new wxStaticText( itemDialog1, wxID_STATIC, _("User:     "), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer10->Add(itemStaticText11, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    conn_user = new wxTextCtrl( itemDialog1, ID_CONN_USER, _T(""), wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE );
    itemBoxSizer10->Add(conn_user, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    //wxBoxSizer* itemBoxSizer13 = new wxBoxSizer(wxHORIZONTAL);
    //itemBoxSizer2->Add(itemBoxSizer13, 0, wxGROW|wxALL, 5);

    wxStaticText* itemStaticText14 = new wxStaticText( itemDialog1, wxID_STATIC, _("Pwd:     "), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer10->Add(itemStaticText14, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    conn_pwd = new wxTextCtrl( itemDialog1, ID_CONN_PWD, _T(""), wxDefaultPosition, wxDefaultSize, wxTE_PASSWORD | wxBORDER_SIMPLE );
    itemBoxSizer10->Add(conn_pwd, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    //wxBoxSizer* itemBoxSizer16 = new wxBoxSizer(wxHORIZONTAL);
    //itemBoxSizer2->Add(itemBoxSizer16, 0, wxGROW|wxALL, 5);


    wxBoxSizer* itemBoxSizer19 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer2->Add(itemBoxSizer19, 0, wxGROW|wxALL, 5);

    wxStaticText* itemStaticText20 = new wxStaticText( itemDialog1, wxID_STATIC, _("DB:       "), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer19->Add(itemStaticText20, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    conn_db = new wxTextCtrl( itemDialog1, ID_CONN_DB, _T(""), wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE );
    itemBoxSizer19->Add(conn_db, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    //wxBoxSizer* itemBoxSizer22 = new wxBoxSizer(wxHORIZONTAL);
    //itemBoxSizer2->Add(itemBoxSizer22, 0, wxGROW|wxALL, 5);

    wxStaticText* itemStaticText23 = new wxStaticText( itemDialog1, wxID_STATIC, _("Save As:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer19->Add(itemStaticText23, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    conn_name = new wxTextCtrl( itemDialog1, ID_CONN_NAME, _T(""), wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE );
    itemBoxSizer19->Add(conn_name, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

	wxBoxSizer* bottomLevel = new wxBoxSizer(wxHORIZONTAL);
	topLevel->Add(bottomLevel,0,wxALIGN_RIGHT, 5);

//    wxBoxSizer* itemBoxSizer25 = new wxBoxSizer(wxHORIZONTAL);
 //   itemBoxSizer2->Add(itemBoxSizer25, 0, wxGROW|wxALL, 5);

    wxButton* itemButton26 = new wxButton( itemDialog1, ID_BT_CONN_SAVE, _("Save && Connect"), wxDefaultPosition, wxDefaultSize, 0 );
    bottomLevel->Add(itemButton26, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxButton* itemButton27 = new wxButton( itemDialog1, ID_BT_CONN, _("Connect!"), wxDefaultPosition, wxDefaultSize, 0 );
    bottomLevel->Add(itemButton27, 0, wxGROW|wxALL, 5);

	bottomLevel->AddSpacer(5);

	s->SplitHorizontally(topBar,panel,60);

}



bool ConnDlg::ShowToolTips()
{
    return true;
}


wxBitmap ConnDlg::GetBitmapResource( const wxString& name )
{
    wxUnusedVar(name);
    return wxNullBitmap;
}

wxIcon ConnDlg::GetIconResource( const wxString& name )
{
    wxUnusedVar(name);
    return wxNullIcon;
}



void ConnDlg::OnConnectBtnClick( wxCommandEvent& event )
{  
	if(_ConnectToMySQL(mysql, conn_host->GetValue(), conn_user->GetValue(), conn_pwd->GetValue(),conn_db->GetValue(), conn_port->GetValue())){
		if ( lsSaved->GetStringSelection() != wxEmptyString )
			serverInfo->conn_name = lsSaved->GetStringSelection();
		else
			serverInfo->conn_name = conn_name->GetValue() == wxEmptyString ? conn_host->GetValue() : conn_name->GetValue();
		EndModal(wxID_OK);
	}
	else{
		wxString errmgs = _GetMySQLError(mysql);
		wxMessageBox(errmgs);
	}
	event.Skip();
}


void ConnDlg::OnBtConnSaveClick( wxCommandEvent& event )
{
	if(_ConnectToMySQL(mysql, conn_host->GetValue(), conn_user->GetValue(), conn_pwd->GetValue(),conn_db->GetValue(), conn_port->GetValue())){
		_SaveConf(conn_name->GetValue(),conn_host->GetValue(),conn_user->GetValue(),conn_pwd->GetValue(),conn_port->GetValue(),conn_db->GetValue());
		serverInfo->conn_name = conn_name->GetValue() == wxEmptyString ? conn_host->GetValue() : conn_name->GetValue();
		EndModal(wxID_OK);
	}
	else{
		wxString errmgs = _GetMySQLError(mysql);
		wxMessageBox(errmgs);
	}
    event.Skip();
}

void ConnDlg::OnListSavedSelected( wxCommandEvent& event )
{
    int selected = event.GetSelection();
    ConfInfo info = array[selected];
    conn_host->SetValue(info.ip);
    conn_user->SetValue(info.user);
    conn_pwd->SetValue(info.pwd);
    conn_port->SetValue(info.port);
    conn_db->SetValue(info.db);
    event.Skip();
}
